<?php
/*
 * @name 广告逻辑处理处理类
 * @author Jin
 * @date 2017.12.21
 **/
namespace app\admin\controller;

use app\common\model\Ad as AdCommonModel;
use think\facade\Session;

class Ad extends Base
{
    /*
     * @name 展示广告
     * @return adlist.html
     **/
    protected $agent_manage_id;
    public function adList()
    {
        //分类
        $ty_list = [0=>'所有广告',1=>'在播广告',2=>'已下播广告',3=>'待播广告',4=>'待审核广告',5=>'审核未通过广告'];
        $inc_type = $this->request->param('inc_type',0);//类别键
        //获取所有广告
//        $data['ad_agents_id'] =Session::get('agent_manage','agent');
//        var_dump($data);die;


        if($inc_type!=0){
            $data['ad_status'] = $inc_type;
            $adlist = AdCommonModel::with('shops,agents')->where($data)->order('ad_start_time','desc')->paginate(5,false,[
                'type'     => 'bootstrap',
            ]);
            $adcount=AdCommonModel::with('shops,agents')->where($data)->count();
        }else{

            $adlist = AdCommonModel::with('shops,agents')->order('ad_start_time','desc')->paginate(5,false,[
                'type'     => 'bootstrap',
            ]);
            $adcount=AdCommonModel::with('shops,agents')->count();

        }


//        dump($list);die;
        //获取所有广告
//        $ad = new AdCommonModel();
//        $adlist = $ad->selectAll(5);
//        $adcount = $ad->count();
//        //将代理商id改为姓名,商户id改为姓名
//        $agent = new \app\common\model\Agents();
//        $shop = new \app\common\model\Shops();
//        foreach ($adlist as $k => $v) {
//            $adlist[$k]['ad_agents_id'] = $agent->getNameByAgentId($v['ad_agents_id']);
//            $adlist[$k]['ad_shop_id'] = $shop->getNameByShopId($v['ad_shop_id']);
//        }
        // dump($adlist);
        // die();

        //将数据发送给模板
        $this->assign('inc_type',$inc_type);
        $this->assign('ty_list',$ty_list);
        $this->assign('adcount', $adcount);
        $this->assign('adlist', $adlist);
        return $this->fetch('adlist');
    }

    /*
     * @name 修改广告状态
     * @return bool true成功,false失败
     **/
    public function stop()
    {
        $adupdate = new \app\admin\model\Ad();
        $res = $this->request->get();
        // 1正在播放2已播出,3已审核未播,4未审核未播,5未通过审核
        if ($res['info'] == 1) {
            return $res = $adupdate->editStatusById($res['id'], 2);
        } elseif ($res['info'] == 2) {
            return $res = $adupdate->editStatusById($res['id'], 3);
        } elseif ($res['info'] == 3) {
            return $res = $adupdate->editStatusById($res['id'], 5);
        }
    }


    /**
     * 添加默认广告
     */
    public function addAds(){
        $label = input('get.label');
        $wz_name = '';
        switch($label){
            case 1: $wz_name = '首页头部轮播';break;
            case 2: $wz_name = '首页文字广告';break;
            case 3: $wz_name = '底部图片广告';break;
        }
        // 表单提交
        if(input('get.act') == 'a'){
            $data = input('post.');
            $data['ad_status'] = 1;
            // 上传图片
            if(stristr('1,3',$data['ad_label'])){
                $rounds = array();
                // 是否上传
                $files = request()->file('image');
                foreach($files as $file){
                    $info = $file->validate(['ext'=>'jpg,jpeg,png'])->move('public/upload/ad');
                    if($info){
                        $rounds[] = $info->getPathName();
                    }else{
                        // 上传失败获取错误信息
                        echo $file->getError();
                    }    
                }
                if(is_array($rounds)) $data['ad_image_url'] = implode(',',$rounds);
            }
            
            // 添加
            $res = db('ad')->insert($data);
            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }

        $this->assign('wz_name',$wz_name);
        return $this->fetch('ad/addAds');   
    }

    // ajax删除广告
    public function delAjax(){
        $id = input('get.id');
        if($id){
            $res = db('ad')->where('ad_id='.$id)->delete();
            return $res;
        }else{
            return 0;
        }
    }
}
